// 封装一个axios的实例
import axios from 'axios';
import { serverHost,port } from '../config/index';
import {Toast} from 'antd-mobile'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css'


// 创建一个新的实例
// 返回promise对象

let r = axios.create({
     baseURL:`${serverHost}:${port}/`,
     withCredentials:'include',
 });
 r.interceptors.request.use((config)=>{
      //console.log('1')
      //Toast.loading('加载中',0)
     NProgress.start();
     //console.log('2');
     return config;
},(error)=>{
     return Promise.reject(error);
});
r.interceptors.response.use((response)=>{
     //console.log('3')
     //Toast.hide();
     NProgress.done();
     //console.log('4')
     return response;
},(error)=>{
     return Promise.reject(error);
})

let request = function (url='',options={}) {
   return function(){
        if (url === '') return Promise.reject('必须传递url');
        // 将promise对象返回去
       return r({
            url,
            method:'get', // method先给一个get请
            
            transformRequest: [function (data) {
               // 对 data 进行任意转换处理
               return data;
             }],
            ...options, // options中有method就会覆盖
        });
         
   }
}



export default request;